﻿@model QualpayCustomerModel

@using Nop.Plugin.Payments.Qualpay
@using Nop.Plugin.Payments.Qualpay.Models.Customer

<nop-panel asp-name="customer-qualpay" asp-icon="fa fa-archive" asp-title="@T("Plugins.Payments.Qualpay.Customer")" asp-hide-block-attribute-name="@QualpayDefaults.HideBlockAttribute" asp-hide="@Model.HideBlock" asp-advanced="true">
    <div class="panel-body">
        @if (!Model.CustomerExists)
        {
            <p>
                @T("Plugins.Payments.Qualpay.Customer.NotExists")
            </p>
            <div class="form-group">
                <div class="col-md-9 col-md-offset-3">
                    <button type="button" id="qualpayCustomerCreate" class="btn btn-info">
                        @T("Plugins.Payments.Qualpay.Customer.Create")
                    </button>

                    <script type="text/javascript">
                        $(document).ready(function () {
                            $('#qualpayCustomerCreate').click(function () {
                                $('#qualpayCustomerCreate').prop('disabled', true);
                                var postData = {
                                    customerId: '@Model.Id'
                                };
                                addAntiForgeryToken(postData);
                                $.ajax({
                                    cache: false,
                                    type: "POST",
                                    url: "@(Url.Action("CreateQualpayCustomer"))",
                                    data: postData,
                                    success: function (data, textStatus, jqXHR) {
                                        location.reload();
                                    },
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        alert('Failed to create customer.');
                                    },
                                    complete: function (jqXHR, textStatus) {
                                        $('#qualpayCustomerCreate').prop('disabled', false);
                                    }
                                });
                            });
                        });
                    </script>
                </div>
            </div>
        }
        else
        {
            <div class="form-group">
                <div class="col-md-3">
                    <nop-label asp-for="QualpayCustomerId" />
                </div>
                <div class="col-md-9">
                    <div class="form-text-row">@Model.QualpayCustomerId</div>
                </div>
            </div>
        }
    </div>

    @if (Model.CustomerExists)
    {
        <div class="panel-body">
            @await Html.PartialAsync("Table", new DataTablesModel
            {
                Name = "qualpay-customer-grid",
                UrlRead = new DataUrl("QualpayCustomerCardList", "Customer", new RouteValueDictionary { [nameof(Model.CustomerCardSearchModel.CustomerId)] = Model.CustomerCardSearchModel.CustomerId }),
                UrlDelete = new DataUrl("QualpayCustomerCardDelete", "Customer", new RouteValueDictionary { [nameof(Model.CustomerCardSearchModel.CustomerId)] = Model.CustomerCardSearchModel.CustomerId }),
                Length = Model.CustomerCardSearchModel.PageSize,
                LengthMenu = Model.CustomerCardSearchModel.AvailablePageSizes,
                Filters = new List<FilterParameter>
                {
                    new FilterParameter(nameof(Model.CustomerCardSearchModel.CustomerId), Model.CustomerCardSearchModel.CustomerId)
                },
                ColumnCollection = new List<ColumnProperty>
                {
                    new ColumnProperty(nameof(QualpayCustomerCardModel.CardId))
                    {
                        Title = T("Plugins.Payments.Qualpay.Customer.Card.Id").Text
                    },
                    new ColumnProperty(nameof(QualpayCustomerCardModel.CardType))
                    {
                        Title = T("Plugins.Payments.Qualpay.Customer.Card.Type").Text
                    },
                    new ColumnProperty(nameof(QualpayCustomerCardModel.MaskedNumber))
                    {
                        Title = T("Plugins.Payments.Qualpay.Customer.Card.MaskedNumber").Text
                    },
                    new ColumnProperty(nameof(QualpayCustomerCardModel.ExpirationDate))
                    {
                        Title = T("Plugins.Payments.Qualpay.Customer.Card.ExpirationDate").Text
                    },
                    new ColumnProperty(nameof(QualpayCustomerCardModel.Id))
                    {
                        Title = T("Admin.Common.Delete").Text,
                        Width = "100",
                        Render = new RenderButtonRemove(T("Admin.Common.Delete").Text),
                        ClassName =  NopColumnClassDefaults.Button
                    }
                }
            })
        </div>
    }
</nop-panel>